<template>
  <div class="activity-card" @click.stop="handleClick">
    <div class="image-box">
      <!-- <img 
        :src="activity.cover" 
        @error="handleImageError" 
        :alt="activity.title"
      /> -->
      <slot name="img"></slot>
      <!-- 标签显示 -->
      <div v-if="activity.tag" class="activity-tag">
        {{ activity.tag }}
      </div>

      <!-- 信息条 -->
      <div class="info-bar">
        <div class="info-item location-item">
          <van-icon name="location-o" class="icon" />
          <span class="location text-ellipsis">{{ activity.location }}</span>
        </div>
        <div class="info-item">
          <van-icon name="clock-o" class="icon" />
          <div class="time-container">
            <div class="time-line">
              <div class="time-point start"></div>
              <div class="time-text">{{ getStartTime }}</div>
            </div>
            <div class="vertical-line"></div>
            <div class="time-line">
              <div class="time-point end"></div>
              <div class="time-text">{{ getEndTime }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="card-content">
      <div class="title">{{ activity.title }}</div>

      <div v-if="activity.description" class="description">
        {{ activity.description }}
      </div>

      <div class="footer">
        <div class="price" :class="{ free: !getPrice() }">
          {{ getPrice() ? `¥${getPrice()}` : '免费' }}
        </div>

        <div class="people">
          <van-icon name="friends-o" />
          <span>{{ activity.participants || activity.current_participants }}/{{ activity.maxParticipants ||
            activity.max_participants }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, defineProps, defineEmits } from 'vue';
import dayjs from 'dayjs';

const props = defineProps({
  activity: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['click'])

const weekDayMap = ['日', '一', '二', '三', '四', '五', '六'];

const formatTimeWithWeekDay = (time: string) => {
  if (!time) return '';

  try {
    const date = dayjs(time);
    const weekDay = weekDayMap[date.day()];
    return date.format(`MM月DD日(周${weekDay}) HH:mm`);
  } catch (error) {
    console.error('Date formatting error:', error);
    return '';
  }
};

const getStartTime = computed(() => {
  const startTime = props.activity.startTime || props.activity.start_time;
  return formatTimeWithWeekDay(startTime);
});

const getEndTime = computed(() => {
  const endTime = props.activity.endTime || props.activity.end_time;
  return formatTimeWithWeekDay(endTime);
});

// 获取价格
const getPrice = () => {
  // 兼容不同的价格字段
  if (props.activity.price !== undefined) {
    return props.activity.price;
  }

  if (props.activity.fee_mode === 'FREE') {
    return 0;
  }

  if (props.activity.fee_mode === 'PAID') {
    return props.activity.fee_amount;
  }

  if (props.activity.fee_mode === 'DEPOSIT') {
    return props.activity.deposit_amount;
  }

  return 0;
};

// 处理图片URL
const getImageUrl = (url) => {
  if (!url) return '';

  // 如果是相对路径，添加API基础URL
  if (url.startsWith('/')) {
    return `${import.meta.env.VITE_APP_API_BASE_URL}${url}`;
  }

  return url;
};

const handleImageError = (e) => {
  // 图片加载失败时使用默认图片
  const img = e.target as HTMLImageElement;
  img.src = 'https://images.unsplash.com/photo-1501555088652-021faa106b9b?auto=format&q=80&w=1200&fit=crop';
  console.log('图片加载失败，使用默认图片');
};

const handleClick = () => {
  console.log('ActivityCard clicked:', props.activity.id)
  emit('click')
}

// 添加调试日志
console.log('Activity data:', props.activity);
console.log('Start time:', props.activity.startTime || props.activity.start_time);
console.log('End time:', props.activity.endTime || props.activity.end_time);
</script>

<style scoped lang="scss">
.activity-card {
  margin-bottom: 4px; // 从16px减小到8px
  // background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
  // height: fit-content;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.image-box {
  position: relative;
}

.image-box img {
  width: 100%;
  display: block;
}

.activity-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(255, 107, 107, 0.7);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.info-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6); // 稍微加深背景色
  color: #fff;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px; // 增加一点间距
}

.info-item {
  display: flex;
  align-items: center;

  .icon {
    font-size: 14px;
    margin-right: 8px;
    opacity: 0.9;
    flex-shrink: 0; // 防止图标缩小
  }

  &.location-item {
    margin-bottom: 2px;
    min-width: 0; // 确保flex布局下文本可以正常省略
  }
}

.time-container {
  flex: 1;
}

.time-line {
  display: flex;
  align-items: center;
  height: 14px; // 增加一点高度

  &:first-child {
    // margin-bottom: 2px; // 两行之间的间距
  }
}

.time-point {
  width: 5px; // 稍微调大点的尺寸
  height: 5px;
  border-radius: 50%;
  margin-right: 8px;

  &.start {
    background-color: #4CAF50;
    box-shadow: 0 0 3px rgba(76, 175, 80, 0.6);
  }

  &.end {
    background-color: #FF5722;
    box-shadow: 0 0 3px rgba(255, 87, 34, 0.6);
  }
}

.vertical-line {
  position: absolute;
  left: 2.5px; // 居中对齐点
  top: 5px;
  width: 1px;
  height: 13px;
  background: rgba(255, 255, 255, 0.4);
}

.time-text {
  font-size: 10px; // 稍微增大字号
  line-height: 1;
  letter-spacing: 0.3px; // 添加字间距
}

.location {
  font-size: 10px;
  line-height: 1;
  letter-spacing: 0.3px;
  flex: 1;
  min-width: 0; // 确保flex布局下文本可以正常省略
}

.card-content {
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 13px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.description {
  font-size: 11px;
  color: #666;
  margin-bottom: 12px;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto; // 将footer推到内容底部
}

.price {
  font-size: 12px;
  font-weight: bold;
  color: #ff6b6b;
}

.price.free {
  color: #40c057;
}

.people {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 12px;
}

.people .van-icon {
  margin-right: 4px;
}

.text-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
